package com.zbkj.service.manager.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.zbkj.dal.base.enums.ExpressOrderPayMethodType;
import com.zbkj.dal.base.enums.ExpressOrderType;
import com.zbkj.dal.base.enums.OrderAddressType;
import com.zbkj.dal.entity.order.OrderAddress;
import com.zbkj.service.manager.ExpressManager;
import com.zbkj.service.model.dto.express.CreateExpressDTO;
import com.zbkj.service.service.OrderAddressService;
import com.zbkj.service.service.OrderDetailService;
import com.zbkj.service.service.OrderService;
import com.zbkj.service.sfexpress.CallExpressNewAPIService;
import com.zbkj.service.sfexpress.order.ContactInfo;
import com.zbkj.service.sfexpress.order.CreateOrderMsgDataRes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class ExpressManagerImpl implements ExpressManager {

    @Lazy
    @Autowired
    private OrderService orderService;
    @Lazy
    @Autowired
    private OrderDetailService orderDetailService;
    @Lazy
    @Autowired
    private OrderAddressService orderAddressService;
    @Autowired
    private CallExpressNewAPIService sfApiService;


    @Override
    public CreateExpressDTO createExpressOrder(String orderId, String productName, ExpressOrderPayMethodType payMethodType, ExpressOrderType expressOrderType) {
        log.info("创建运单参数 {},{},{}", orderId, payMethodType, expressOrderType);
        // 先创建一个物流单，得到物流单号，记录本次物流类型
        String expressOrderNo = SecureUtil.md5(orderId + expressOrderType.name());
        // 获取订单地址
        final List<OrderAddress> orderAddressList = orderAddressService.getOrderAddressList(orderId, expressOrderType.name());
        final List<ContactInfo> contactInfoList = new ArrayList<>();
        // 寄方信息
        Optional<OrderAddress> sendOptional = orderAddressList.stream().filter(it -> OrderAddressType.isSend(it.getAddressType())).findFirst();
        Assert.isTrue(sendOptional.isPresent(), "寄方信息不能为空");
        contactInfoList.add(this.buildContactInfo(sendOptional.get(), OrderAddressType.SHIPPER));
        // 到方信息
        Optional<OrderAddress> receiveOptional = orderAddressList.stream().filter(it -> OrderAddressType.isReceive(it.getAddressType())).findFirst();
        Assert.isTrue(receiveOptional.isPresent(), "到方信息不能为空");
        contactInfoList.add(this.buildContactInfo(receiveOptional.get(), OrderAddressType.RECEIVER));
        // 请求创建物流信息
        CreateOrderMsgDataRes order = sfApiService.createOrder(expressOrderNo, productName, null, null, payMethodType, contactInfoList);
        Assert.notNull(order, "创建物流单号失败");
        return new CreateExpressDTO(expressOrderNo, order.getExpressNo());
    }

    @Override
    public CreateExpressDTO createExpressOrderWithPickupCode(String orderId, String productName, LocalDateTime pickupTime, ExpressOrderPayMethodType payMethodType, ExpressOrderType expressOrderType) {
        log.info("创建运单参数 {},{},{}", orderId, payMethodType, expressOrderType);
        // 获取到订单信息
//        String expressOrderNo = SecureUtil.md5(orderId + expressOrderType.name());
        String expressOrderNo = "TEST" + RandomUtil.randomString(orderId + expressOrderType.name(), 24);
        // 获取订单地址
        final List<OrderAddress> orderAddressList = orderAddressService.getOrderAddressList(orderId, expressOrderType.name());
        final List<ContactInfo> contactInfoList = new ArrayList<>();
        // 寄方信息
        Optional<OrderAddress> sendOptional = orderAddressList.stream().filter(it -> OrderAddressType.isSend(it.getAddressType())).findFirst();
        Assert.isTrue(sendOptional.isPresent(), "寄方信息不能为空");
        contactInfoList.add(this.buildContactInfo(sendOptional.get(), OrderAddressType.SHIPPER));
        // 到方信息
        Optional<OrderAddress> receiveOptional = orderAddressList.stream().filter(it -> OrderAddressType.isReceive(it.getAddressType())).findFirst();
        Assert.isTrue(receiveOptional.isPresent(), "到方信息不能为空");
        contactInfoList.add(this.buildContactInfo(receiveOptional.get(), OrderAddressType.RECEIVER));
        // 请求创建物流信息
        String senderPickupCode = RandomUtil.randomNumbers(4);
        CreateOrderMsgDataRes order = sfApiService.createOrder(expressOrderNo, productName, pickupTime, senderPickupCode, payMethodType, contactInfoList);
        Assert.notNull(order, "创建物流单号失败");
        return new CreateExpressDTO(expressOrderNo, order.getExpressNo(), senderPickupCode);
    }

    @Override
    public void printWayBills(Collection<String> expressNumber) {
        log.info("云打印电子面单参数 {}", expressNumber);
        sfApiService.printWayBills(expressNumber);
    }

    private ContactInfo buildContactInfo(OrderAddress orderAddress, OrderAddressType contactType) {
        ContactInfo contactInfo = new ContactInfo();
        contactInfo.setContact(orderAddress.getContactName());
        contactInfo.setMobile(orderAddress.getContactPhone());
        contactInfo.setProvince(orderAddress.getProvinceName());
        contactInfo.setCity(orderAddress.getCityName());
        contactInfo.setCounty(orderAddress.getRegionName());
        contactInfo.setAddress(orderAddress.getAddress());
        contactInfo.setContactType(contactType.getType());
        return contactInfo;
    }
}
